package com.tangbao.comment.service.impl;

import com.tangbao.comment.mapper.CommentMapper;
import com.tangbao.comment.model.Comment;
import com.tangbao.comment.service.CommentService;
import com.tangbao.util.Page;
import com.tangbao.util.ResEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.core.Response;
import java.util.Date;
import java.util.List;

/**
 * 功能:评论服务
 * 创建人:zhouxin
 * 创建时间:2018/9/29
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    CommentMapper commentMapper;

    /**
     * 查询全部评论
     * @param start
     * @param pageSize
     * @return
     */
    @Override
    public Response getCommentByAll(Integer start, Integer pageSize) {
        List<Comment> allComments = commentMapper.getCommentByAll(start,pageSize);
        ResEntity entity = ResEntity.okEntity().put("allComments", allComments);
        return Response.ok(entity).build();
    }
    /**
     * 分页根据资讯id查询相关评论
     * @param artId
     * @param currentPage 当前页
     * @param pageSize 页大小
     * @return
     */
    @Override
    public Response getCommentsByArtId(String commentType,Integer artId,Integer currentPage, Integer pageSize) {
        Page page = new Page();
        if(null != currentPage) {
            page.setCurrentPage(currentPage);
        }
        if(null != pageSize) {
            page.setPageSize(pageSize);
        }

        List<Comment> allComments = commentMapper.getCommentsByArtId(commentType,artId,page.getStart(),page.getPageSize());
        //获取评论总数
        Integer allCommentsCount = commentMapper.getCommentsNumByArtId(commentType,artId);
        page.setList(allComments);
        page.setTotalCount(allCommentsCount);
        ResEntity entity = ResEntity.okEntity().put("page", page);
        return Response.ok(entity).build();
    }

    @Override
    public Response getCommentById(String commentType,String id) {
        Comment comments = commentMapper.getCommentById(Integer.parseInt(id),commentType);
        ResEntity entity = ResEntity.okEntity().put("comments", comments);
        return Response.ok(entity).build();
    }

    public Response saveComment(String commentType,Integer artId,Comment comment) {
        comment.setAddTime(new Date());
        if(null != artId && artId!=0) {
            comment.setArtId(artId);
        }
        if(null != artId && artId!=0) {
            comment.setCommentType(commentType);
        }else{
            ResEntity entity = ResEntity.errorEntity("评论类型不能为空");
            return Response.ok(entity).build();
        }
        int size = commentMapper.saveComment(comment);
        ResEntity entity = ResEntity.okEntity().put("size", size);
        return Response.ok(entity).build();
    }

    public Response updateComment(String commentType,Integer artId,Comment comment) {

        if(null != artId && artId!=0) {
            comment.setArtId(artId);
        }
        if(null != artId && artId!=0) {
            comment.setCommentType(commentType);
        }else{
            ResEntity entity = ResEntity.errorEntity("评论类型不能为空");
            return Response.ok(entity).build();
        }

        int size = commentMapper.updateComment(comment);
        ResEntity entity = ResEntity.okEntity().put("size", size);
        return Response.ok(entity).build();
    }


}
